﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Nop.Admin.Models.WindAlert;
using Nop.Core;
using Nop.Services.Helpers;
using Nop.Services.Localization;
using Nop.Services.Security;
using Nop.Services.WindAlert;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Kendoui;

namespace Nop.Admin.Controllers
{
    public partial class WForecastDataController : BaseAdminController
    {
        private readonly IForecastDataService _forecastDataService;
        private readonly IWorkContext _workContext;
        private readonly ILocalizationService _localizationService;
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly IPermissionService _permissionService;
        private readonly ISpotService _spotService;

        public WForecastDataController(IForecastDataService forecastDataService, IWorkContext workContext,
            ILocalizationService localizationService, IDateTimeHelper dateTimeHelper,
            IPermissionService permissionService, ISpotService SpotService)
        {
            this._forecastDataService = forecastDataService;
            this._workContext = workContext;
            this._localizationService = localizationService;
            this._dateTimeHelper = dateTimeHelper;
            this._permissionService = permissionService;
            this._spotService = SpotService;
        }

        #region methods
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();
            var model = new ForecastDataListModel();
            //spots
            model.AvailableSpots.Add(new SelectListItem() { Text = _localizationService.GetResource("Admin.Common.All"), Value = "0" });
            foreach (var s in _spotService.GetAllSpots(isForecast: true))
                model.AvailableSpots.Add(new SelectListItem() { Text = s.Name, Value = s.Id.ToString() });
            return View(model);
        }

        [HttpPost]
        public ActionResult ForecastDataList(DataSourceRequest command, ForecastDataListModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            var forecastDataItems = _forecastDataService.GetAllForecastData(command.Page - 1, command.PageSize, model.SearchSpotId);
            var gridModel = new DataSourceResult
            {
                Data = forecastDataItems.Select(x =>
                {
                    return new ForecastDataModel()
                    {
                        Id = x.Id,
                        CreatedOnUtc = x.CreatedOnUtc,
                        ForecastTime = x.ForecastTime,
                        Height = x.Height,
                        Period = x.Period,
                        WaveDirection = x.WaveDirection,
                        WindDirection = x.WindDirection,
                        WindSpeed = x.WindSpeed,
                        Forecast = x.Name,
                        SpotId = (int)x.SpotId,
                    };
                }),
                Total = forecastDataItems.TotalCount
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        [HttpPost, ActionName("List")]
        [FormValueRequired("clearall")]
        public ActionResult ClearAll()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            _forecastDataService.ClearAll();

            SuccessNotification(_localizationService.GetResource("Admin.System.Log.Cleared"));
            return RedirectToAction("List");
        }

        public ActionResult View(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            var forecastData = _forecastDataService.GetForecastDataById(id);
            if (forecastData == null)
                //No forecastData found with the specified id
                return RedirectToAction("List");

            var model = new ForecastDataModel()
            {
                Id = forecastData.Id,
                CreatedOnUtc = forecastData.CreatedOnUtc,
                ForecastTime = forecastData.ForecastTime,
                WindSpeed = forecastData.WindSpeed,
                WindDirection = forecastData.WindDirection,
                WaveDirection = forecastData.WaveDirection,
                Period = forecastData.Period,
                Height = forecastData.Height,
                Forecast = forecastData.Name,
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            var forecastData = _forecastDataService.GetForecastDataById(id);
            if (forecastData == null)
                //No forecastata found with the specified id
                return RedirectToAction("List");

            _forecastDataService.DeleteForecastData(forecastData);

            SuccessNotification(_localizationService.GetResource("Admin.System.Log.Deleted"));
            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult DeleteSelected(ICollection<int> selectedIds)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            if (selectedIds != null)
            {
                var forecastDataItems = _forecastDataService.GetForecastDataByIds(selectedIds.ToArray());
                foreach (var forecastDataItem in forecastDataItems)
                    _forecastDataService.DeleteForecastData(forecastDataItem);
            }

            return Json(new { Result = true });
        }
        #endregion


    }
}